/* 路由抽离专属文件 */

//下载完之后导入路由
import VueRouter from 'vue-router'
import Vue from 'vue'

import login from '../views/login/login.vue'
import layout from '@/views/layout/layout.vue'
import chart from '@/views/layout/chart/chart.vue'
import user from '@/views/layout/user/user.vue'
import question from '@/views/layout/question/question.vue'
import business from '@/views/layout/business/business.vue'
import subject from '@/views/layout/subject/subject.vue'

// 导入进度条插件
import Nprogress from 'nprogress'
// 导入进度条样式
import 'nprogress/nprogress.css'

// 注册使用路由
Vue.use(VueRouter);

// 实例化路由
const router = new VueRouter({
    routes: [
        { path: '/login', component: login },
        {
            path: '/', redirect: '/login',
            meta:{title:'登录'}
        },
        {
            path: '/layout',
            component: layout,
            redirect: '/layout/chart',
            children: [
                {
                    path: '/layout/chart', component: chart,
                    meta:{title:'数据概览'}
                },
                {
                    path: '/layout/user', component: user,
                    meta:{title:'用户列表'}
                },
                {
                    path: '/layout/question', component: question,
                    meta:{title:'题库列表'}
                },
                {
                    path: '/layout/business', component: business,
                    meta:{title:'企业列表'}
                },
                {
                    path: '/layout/subject', component: subject,
                    meta:{title:'学科列表'}
                },
            ]
        }
    ]
});

// 前置守卫：还没有出form的路由
router.beforeEach((to, from, next) => {
    // window.console.log("to:", to);
    // window.console.log("from:", from);
    // window.console.log("next:", next);
    // 进度条开始位置
    Nprogress.start();
    next() // 正常通过   不正常通过 next(path)
});

// 后置守卫：已经入to的路由
router.afterEach((to, from) => {
    // to:要去的路由的路由信息,相当于相应路由的this.$route
    // from：从哪来的路由的路由信息,相当于相应路由的this.$route
    window.console.log(from);
    // 修改顶部标题
    // document.getElementById('title').innerText = '黑马面面---' + to.meta.title;
    document.title = '黑马面面---' + to.meta.title; // 上面的简写
    // 进度条结束位置,设置定时器是为了让进度条看起来明显一点,实际开发不用
    setTimeout(() => {
        Nprogress.done()
    },1000)
});

// 解决新版本vue重复点击跳转报错的问题
const originalReplace = VueRouter.prototype.push;
VueRouter.prototype.push = function push (location) {
  return originalReplace.call(this, location).catch(err => err)
};

// 暴露router
export default router